package xyz.zdsoft.auth_module.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import xyz.zdsoft.auth_module.mapper.AuthMapper;
import xyz.zdsoft.auth_module.result.User;
import xyz.zdsoft.auth_module.service.AuthService;
import xyz.zdsoft.auth_module.utils.CheckCode;
import xyz.zdsoft.auth_module.utils.UserList;
import xyz.zdsoft.auth_module.vo.CheckUser;
import xyz.zdsoft.common.enums.CommonEnum;
import xyz.zdsoft.common.exception.BizException;
import xyz.zdsoft.common.utils.JwtUtils;
import xyz.zdsoft.common.utils.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceImpl implements AuthService {

    private final AuthMapper authMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public AuthServiceImpl(AuthMapper authMapper) {
        this.authMapper = authMapper;
    }

    @Override
    public Result login(String userNo, String password, String roleType,String checkCode,String checkId) {
        //判断userType是否符合系统预定义
        if(userNo==null||userNo=="") return Result.error().message("请输入用户名");
        if(password==null||password=="") return Result.error().message("请输入密码");
        if(roleType==null||roleType=="") return Result.error().message("用户类型错误");
        if(checkId==null||checkId=="") return Result.error().message("请输入验证码");
        if(!JwtUtils.checkCookie(checkId)) return Result.error().message("验证码已过期");
        String code = JwtUtils.getMemberCheckCodeByJwtToken(checkId);
        if(!code.equals(checkCode)) return Result.error().message("验证码输入错误");
        User user = authMapper.selectUserByNo(userNo,roleType);
        if(user==null) return Result.error().message("账号不存在");
        if(!user.getPassword().equals(password)) return Result.error().message("密码错误");
//        JwtUtils.Id =user.getId().toString();

        String userType = "";
        if(roleType.equals("0")){
            userType = "admin";
        }else if(roleType.equals("1")){
            userType = "student";
        }else{
            userType = "teacher";
        }
//        JwtUtils.UserType = userType;

        CheckUser checkUser = new CheckUser(
                JwtUtils.getJwtToken(user.getId().toString(),userType),
                user.getIsFirstLogin(),
                user.getUserNo(),
                user.getName()
        );
        //将用户信息存在内存中(现在是存在Redis里的)
//        UserList.getInstance().addUser(JwtUtils.getJwtToken(user.getId().toString(),userType),checkUser);
   //单用户登录
    /*Set<String> keys = redisTemplate.keys("*");
        for (String s : keys) {
            Object obj = redisTemplate.opsForValue().get(s);
            CheckUser checkUser1 = JSON.parseObject(obj.toString(), CheckUser.class);
            if(checkUser1.getUserNo().equals(checkUser.getUserNo())) redisTemplate.delete(s);
        }*/
        redisTemplate.opsForValue().set(JwtUtils.getJwtToken(user.getId().toString(),userType),checkUser,60 * 24, TimeUnit.MINUTES);
        return Result.ok().message("登录成功").data(checkUser);
    }

    @Override
    public Result loginOut(HttpServletRequest request) {
//        UserList.getInstance().removeUser(JwtUtils.getToken(request));
        redisTemplate.delete(JwtUtils.getToken(request));
        return Result.ok().message("退出成功");
    }

}
